import { asyncRouterMap, constantRouterMap } from '@/router'
/* Layout */
import Layout from '@/views/layout/Layout'

/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */

function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.indexOf(role) >= 0)
  } else {
    return true
  }
}

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param asyncRouterMap
 * @param roles
 */

function filterAsyncRouter(asyncRouterMap, roles) {
  const accessedRouters = asyncRouterMap.filter(route => {
    if (hasPermission(roles, route)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children, roles)
      }
      return true
    }
    return false
  })
  return accessedRouters
}

// 循环遍历后端路由表

function filterRouters(data) {
  const accessedRouters = []
  data.functionsList.forEach(function(firstValue) {
    const firstChildren = []
    firstValue.childFunctionList.forEach(function(secondValue) {
      const secondChildren = []
      if (secondValue.childFunctionList !== null) {
        secondValue.childFunctionList.forEach(function(thirdValue) {
          const thirdChildren = []
          if (thirdValue.childFunctionList !== null) {
            thirdValue.childFunctionList.forEach(function(fourthValue) {
              const fourthChildren = []
              if (fourthValue.childFunctionList !== null) {
                fourthValue.childFunctionList.forEach(function(fifthValue) {
                  fourthChildren.push({
                    path: fifthValue.path,
                    name: fifthValue.path,
                    component: () => import(`@/views${fifthValue.url}/index`),
                    meta: {
                      title: fifthValue.name
                    }
                  })
                })
                const routerPath = fourthValue.childFunctionList[0].path.split('/')
                thirdChildren.push({
                  path: '/' + fourthValue.path,
                  redirect: `/${fourthValue.path}/${routerPath[routerPath.length - 1]}`,
                  name: fourthValue.path,
                  component: () => import(`@/views/${fourthValue.path}/index`),
                  meta: {
                    title: fourthValue.name
                  },
                  children: fourthChildren
                })
              } else {
                thirdChildren.push({
                  path: fourthValue.path,
                  name: fourthValue.path,
                  component: () => import(`@/views${fourthValue.url}/index`),
                  meta: {
                    title: fourthValue.name
                  }
                })
              }
            })
            const routerPath = thirdValue.childFunctionList[0].path.split('/')
            secondChildren.push({
              path: '/' + thirdValue.path,
              redirect: `/${thirdValue.path}/${routerPath[routerPath.length - 1]}`,
              name: thirdValue.path,
              component: () => import(`@/views/${thirdValue.path}/index`),
              meta: {
                title: thirdValue.name
              },
              children: thirdChildren
            })
          } else {
            secondChildren.push({
              path: thirdValue.path,
              name: thirdValue.path,
              component: () => import(`@/views${thirdValue.url}/index`),
              meta: {
                title: thirdValue.name
              }
            })
          }
        })
        const routerPath = secondValue.childFunctionList[0].path.split('/')
        firstChildren.push({
          path: '/' + secondValue.path,
          redirect: `/${secondValue.path}/${routerPath[routerPath.length - 1]}`,
          name: secondValue.path,
          component: () => import(`@/views/${secondValue.path}/index`),
          meta: {
            title: secondValue.name
          },
          children: secondChildren
        })
      } else {
        firstChildren.push({
          path: secondValue.path,
          name: secondValue.path,
          component: () => import(`@/views${secondValue.url}/index`),
          meta: {
            title: secondValue.name
          }
        })
      }
    })
    const routerPath = firstValue.childFunctionList[0].path.split('/')
    accessedRouters.push({
      path: '/' + firstValue.path,
      redirect: `/${firstValue.path}/${routerPath[routerPath.length - 1]}`,
      component: Layout,
      name: firstValue.path,
      meta: {
        title: firstValue.name,
        icon: firstValue.path
      },
      children: firstChildren
    })
  })
  accessedRouters.push({
    path: '*',
    redirect: '/404',
    hidden: true
  })
  return accessedRouters
}

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({ commit }, data) {
      return new Promise(resolve => {
        // const { roles } = data
        // let accessedRouters
        // if (roles.indexOf('edit') >= 0) {
        //   accessedRouters = asyncRouterMap
        // } else {
        //   accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
        // }
        const accessedRouters = filterRouters(data)
        commit('SET_ROUTERS', accessedRouters)
        resolve()
      })
    }
  }
}

export default permission
